$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/jxstudentschedule/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true},
            {label: '学生名字', name: 'studentName', index: 'student_id', width: 80},
            {label: '教室名称', name: 'laboratoryName', index: 'laboratory_id', width: 40},
            {label: '设备名称', name: 'deviceName', index: 'use_device_id', width: 40},
            {label: '课程名称', name: 'courseName', index: 'course_schedule_id', width: 40},
            {label: '授课日期', name: 'courseDay', index: 'course_schedule_id', width: 80},
            {
                label: '实验时间',
                name: 'courseDay',
                index: 'course_schedule_id',
                width: 100,
                formatter: function (cellvalue, options, rowObject) {
                    console.log(rowObject);
                    var begainTime = rowObject.begainTime;
                    var endTime = rowObject.endTime;
                    return begainTime + "到" + endTime
                }
            },
            {
                label: '学习状态',
                name: 'studyStatus',
                index: 'study_status',
                width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    if (cellvalue == 0) {
                        return "未授课";
                    } else if (cellvalue == 1) {
                        return "授课中";
                    } else if (cellvalue == 2){
                        return "授课完成";
                    } else{
                        return "逾期授课";
                    }
                }
            },
            {
                label: '操作',
                name: 'id',
                index: 'id',
                width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    var studyStatus = rowObject.studyStatus;
                    var courseScheduleId= rowObject.courseScheduleId;
                    var id=rowObject.id;
                    if (studyStatus == "0") {
                        return "<a class='btn btn-default' onclick='mixDevice("+id+","+courseScheduleId+ ")' ><i class='fa fa-desktop' style='font-size: 20px'></i>&nbsp;调配</a>";
                    }
                    return "";

                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        shrinkToFit:true,
        autoScroll: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
           // $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "scroll"});
        }
    });

    $('#courseDay').datetimepicker({
        language: 'zh-CN',
        todayBtn: 1,
        autoclose: 1,
        minView: 2,
        format: "yyyy-mm-dd",
        startDate: "2000-01-01 00:00:00",// 开始时间
    });


    vm.getAllLaboratory();   //  拿到所有的课程
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            courseDay: null,
            laboratoryId: "",
            studentName: null,
            courseName: null,
            batch: null
        },
        allCourse: [{"name": "请选择", "id": ""}],
        allTeacher: [{"userName": "请选择", "userId": ""}],
        allLaboratory: [{"name": "请选择", "id": ""}],
        showList: true,
        title: null,
        studentScheduleId: "", //  需要调配的日程id
        courseScheduleId:"",
        selectDeviceId:"",  // 选中的设备
        mixLaboratory:[{id:0,name:""}],
        mixDevices:[{id:0,name:""}], // 可调配的设备
        selectLaboratorys:{},
        selectLaboratory:{},  // 选中的实验室
        showDevice:false, // 显示可选设备
        selectDeviceId:0,
        jxStudentSchedule: {}
    },
    methods: {
        closeAll:function(){   //  关闭所有
            console.log("执行关闭");
          var  url="sys/screenControl/closeAll";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                success: function(r){
                    if(r.code === 0){
                        layer.msg("执行成功！")
                        vm.reload();
                    }
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.jxStudentSchedule = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            vm.jxStudentSchedule.useDeviceId=vm.selectDeviceId;
            console.log(vm.jxStudentSchedule);
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {
                var url = vm.jxStudentSchedule.id == null ? "sys/jxstudentschedule/save" : "sys/jxstudentschedule/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.jxStudentSchedule),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                        location.reload();
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/jxstudentschedule/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/jxstudentschedule/info/" + id, function (r) {
                vm.jxStudentSchedule = r.jxStudentSchedule;
            });
        }, getAllLaboratory: function () {  // 拿到所有实验室
            $.get(baseURL + "sys/jxlaboratory//allList", function (r) {
                console.log(r);
                vm.allLaboratory = r.list;
            });
        },changeLaboratory:function(e){
            var val = e.target.value;
                vm.selectLaboratory = vm.selectLaboratorys[val]; //     选择了实验室
                if(vm.selectLaboratory){
                var courseScheduleId = vm.selectLaboratory.id;
                vm.jxStudentSchedule.courseScheduleId = courseScheduleId; //  更改日程
                $.get(baseURL + "sys/jxcourseschedule/mixDevices/" + courseScheduleId, function (r) {
                    if (r.code == 0) {
                        vm.mixDevices = r.data;
                        console.log(r.data);
                        if (vm.mixDevices.length > 0) {
                            vm.showDevice = true;
                        }
                    }

                });
            }else{
             vm.selectLaboratory={};
             vm.showDevice = false;
             vm. mixDevices=[{id:0,name:""}];
            }

        },
        reload: function (event) {
            vm.q.courseDay = $("#courseDay").val();
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page,
                postData: {
                    'courseDay': vm.q.courseDay,
                    'laboratoryId': vm.q.laboratoryId,
                    'studentName': vm.q.studentName,
                    'courseName': vm.q.courseName,
                    'batch': vm.q.batch
                }
            }).trigger("reloadGrid");

            location.reload();
        }
    }
});

function mixDevice( id,courseScheduleId) {  //   调配设备
    vm.showList = false;
    vm.studentScheduleId=id;
    vm.getInfo(id);
    getmixLaboratory(courseScheduleId);   //  允许调配的实验室
}

function getmixLaboratory(courseScheduleId) {  //  选择允许体哦啊配的实验室
    vm.showDevice=false;
    vm.mixLaboratory=[{id:0,name:""}];
    $.get(baseURL + "sys/jxcourseschedule/mixLaboratory/" + courseScheduleId, function (r) {
        if(r.code==0){
         vm.mixLaboratory=r.data;
         if(vm.mixLaboratory.length>0){
             vm.mixLaboratory.forEach(function(v,i,arr){
              vm.selectLaboratorys[v.id]=v;
             })
         }else{
             layer.alert("没有合适的实验室调配，请重新预约时间段！")
         }
        }

    });

}